﻿using System;
using System.Windows.Forms;

namespace ComTools
{
    using System.Drawing;
    using System.Threading;
    using IronBarCode;
    public partial class frmControllerNodes : Form
	{
		public frmControllerNodes()
		{
			InitializeComponent();
			hook = new BarCodeHooK();
		}

		BarCodeHooK hook;

        private void AppendTxt(string txt)
        {
            label2.Text += Environment.NewLine + txt;
        }

        private void frmControllerNodes_Load(object sender, EventArgs e)
        {
            hook.BarCodeEvent += Hook_BarCodeEvent;
            hook.Start();
        }

        private void Hook_BarCodeEvent(BarCodeHooK.BarCodes barCode)
        {
            if (barCode.IsValid)
            {
                hook.Stop();

                label2.Text = barCode.BarCode.Trim();

                Image image = IronBarCode.BarcodeWriter.CreateBarcode(barCode.BarCode.Trim(), BarcodeEncoding.Code128).Image;
                pictureBox1.Image = image;
                Application.DoEvents();

                Form form = new Form();
                form.Parent = this.Parent;
                form.Show();

                this.Hide();
                //new System.Threading.Thread(new System.Threading.ThreadStart(() => {
                //Form form = new Form();
                //form.Show();
                //})).Start();
            }
        }

        private void frmControllerNodes_FormClosed(object sender, FormClosedEventArgs e)
        {
            hook.Stop();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread thread = new Thread(new ThreadStart(() => {
                Form form = new Form();
                form.Show(this);
            }));
            thread.Start();
        }
    }
}